//
//  ActivityInfoViewController.m
//  MemberProject
//
//  Created by mac on 15/7/8.
//  Copyright (c) 2015年 MAC OS X. All rights reserved.
//

#import "ActivityInfoViewController.h"
#import "InfoTableViewCell.h"
#import "VolunteerInputViewController.h"
#import "ActivityInfoTableViewCell.h"
#import "ActivityInfoWhiteTableViewCell.h"
@interface ActivityInfoViewController ()<UITableViewDataSource,UITableViewDelegate,ActivityInfoTableViewCellDelegate>
@property(strong,nonatomic)UITableView *tableView;
@property(strong,nonatomic)NSArray *data;
@property(strong,nonatomic)NSArray *buttonTitles;
@end

@implementation ActivityInfoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title=@"活动信息";
    
    NSMutableArray *arrM=[NSMutableArray array];
    if(self.resultDic[@"activityCode"]){
        [arrM addObject:@{@"key":@"活动代码:",@"name":self.resultDic[@"activityCode"]}];
    }
    if(self.resultDic[@"activityName"]){
        [arrM addObject:@{@"key":@"活动名称:",@"name":self.resultDic[@"activityName"]}];
    }
    if(self.resultDic[@"groupCode"]){
        [arrM addObject:@{@"key":@"主办方代码:",@"name":self.resultDic[@"groupCode"]}];
    }
    if(self.resultDic[@"groupName"]){
        [arrM addObject:@{@"key":@"主办方名称:",@"name":self.resultDic[@"groupName"]}];
    }
    self.data=arrM;
    
    self.buttonTitles=@[@"补登",@"报名",@"签到",@"签退"];
//    self.edgesForExtendedLayout = UIRectEdgeNone;
//    self.automaticallyAdjustsScrollViewInsets=NO;
    
    float height=[Utils heightForLabel:[self.data lastObject][@"groupName"] andFont:17.0f andWidth:SCREEN_WIDTH-100-12-8-10];
    if(height<50){
        height=50;
    }
    
    
    UITableView *tableView=[[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT) style:UITableViewStylePlain];
    tableView.delegate=self;
    tableView.dataSource=self;
    [self.view addSubview:tableView];
    self.tableView=tableView;
    tableView.separatorStyle=UITableViewCellSeparatorStyleNone;
    [tableView registerNib:[UINib nibWithNibName:@"InfoTableViewCell" bundle:nil] forCellReuseIdentifier:@"CellIdentifier"];
    
    [tableView registerNib:[UINib nibWithNibName:@"ActivityInfoTableViewCell" bundle:nil] forCellReuseIdentifier:@"CellIdentifierButton"];
    
    
    [tableView registerNib:[UINib nibWithNibName:@"ActivityInfoWhiteTableViewCell" bundle:nil] forCellReuseIdentifier:@"CellIdentifierWhite"];

    UIView *footerView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 20)];
    tableView.tableFooterView=footerView;
    /*
    UIButton *inputButton=[UIButton buttonWithType:UIButtonTypeCustom];
    inputButton.frame=CGRectMake(5, tableView.y+tableView.height+6, SCREEN_WIDTH-10, 30);
    [inputButton addTarget:self action:@selector(chickInputButton:) forControlEvents:UIControlEventTouchUpInside];
    [inputButton setTitle:@"补登" forState:UIControlStateNormal];
    [inputButton setTitleColor:[UIColor colorWithRed:64/255.0 green:174/255.0 blue:249/255.0 alpha:1] forState:UIControlStateNormal];
    inputButton.layer.borderWidth=0.5f;
    inputButton.layer.borderColor=[UIColor colorWithRed:64/255.0 green:174/255.0 blue:249/255.0 alpha:1].CGColor;
    inputButton.layer.cornerRadius=2.5f;
    [self.view addSubview:inputButton];
    
    
    UIButton *applyButton=[UIButton buttonWithType:UIButtonTypeCustom];
    applyButton.frame=CGRectMake(5, inputButton.y+inputButton.height+6, SCREEN_WIDTH-10, 30);
    [applyButton addTarget:self action:@selector(chickApplyButton:) forControlEvents:UIControlEventTouchUpInside];
    [applyButton setTitle:@"报名" forState:UIControlStateNormal];
    [applyButton setTitleColor:[UIColor colorWithRed:64/255.0 green:174/255.0 blue:249/255.0 alpha:1] forState:UIControlStateNormal];
    applyButton.layer.borderWidth=0.5f;
    applyButton.layer.borderColor=[UIColor colorWithRed:64/255.0 green:174/255.0 blue:249/255.0 alpha:1].CGColor;
    applyButton.layer.cornerRadius=2.5f;
    [self.view addSubview:applyButton];
    
    
    UIButton *signInButton=[UIButton buttonWithType:UIButtonTypeCustom];
    signInButton.frame=CGRectMake(5, applyButton.y+applyButton.height+6, SCREEN_WIDTH-10, 30);
    [signInButton addTarget:self action:@selector(chickSignInButton:) forControlEvents:UIControlEventTouchUpInside];
    [signInButton setTitle:@"签到" forState:UIControlStateNormal];
    [signInButton setTitleColor:[UIColor colorWithRed:64/255.0 green:174/255.0 blue:249/255.0 alpha:1] forState:UIControlStateNormal];
    signInButton.layer.borderWidth=0.5f;
    signInButton.layer.borderColor=[UIColor colorWithRed:64/255.0 green:174/255.0 blue:249/255.0 alpha:1].CGColor;
    signInButton.layer.cornerRadius=2.5f;
    [self.view addSubview:signInButton];
    
    
    UIButton *exitButton=[UIButton buttonWithType:UIButtonTypeCustom];
    exitButton.frame=CGRectMake(5, signInButton.y+signInButton.height+6, SCREEN_WIDTH-10, 30);
    [exitButton addTarget:self action:@selector(chickExitButton:) forControlEvents:UIControlEventTouchUpInside];
    [exitButton setTitle:@"签退" forState:UIControlStateNormal];
    [exitButton setTitleColor:[UIColor colorWithRed:64/255.0 green:174/255.0 blue:249/255.0 alpha:1] forState:UIControlStateNormal];
    exitButton.layer.borderWidth=0.5f;
    exitButton.layer.borderColor=[UIColor colorWithRed:64/255.0 green:174/255.0 blue:249/255.0 alpha:1].CGColor;
    exitButton.layer.cornerRadius=2.5f;
    [self.view addSubview:exitButton];
     */
}

-(void)chickExitButton:(UIButton *)sender{
    ModelUser *user=[UserInfoManager sharedManager].modelMser;
    NSDateFormatter *f=[[NSDateFormatter alloc]init];
    [f setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];
    [f setDateFormat:@"yyyyMMdd HHmmss"];
    NSString *dateStr=[f stringFromDate:[NSDate date]];
    [SVProgressHUD show];
    [[BuaatVolunteerServerSDK sharedInstance]requestActivitySignWithActivityCode:self.activityCode volunteerCode:user.volunteerCode signType:@"2" signTime:dateStr signLongitude:@"" signDimensions:@"" success:^(NSHTTPURLResponse *response, id responseObject) {
        if([responseObject[@"ret"] intValue]==0){
            [SVProgressHUD showSuccessWithStatus:@"签退成功"];
        }
    } failure:^(NSHTTPURLResponse *response, NSError *error) {
        [SVProgressHUD showErrorWithStatus:@"网络错误"];
    }];
}

-(void)chickApplyButton:(UIButton *)sender{
    ModelUser *user=[UserInfoManager sharedManager].modelMser;
    NSDateFormatter *f=[[NSDateFormatter alloc]init];
    [f setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];
    [f setDateFormat:@"yyyyMMdd HHmmss"];
    NSString *dateStr=[f stringFromDate:[NSDate date]];
    [SVProgressHUD show];
    [[BuaatVolunteerServerSDK sharedInstance]requestActivityRegisterWithActivityCode:self.activityCode volunteerCode:user.volunteerCode regStatus:@"1" regTime:dateStr success:^(NSHTTPURLResponse *response, id responseObject) {
        if([responseObject[@"ret"] intValue]==0){
            [SVProgressHUD dismiss];
            NSString *str=[[NSString alloc]initWithFormat:@"报名成功\n报名号:%@",responseObject[@"result"][@"regId"]];
            UIAlertView *alertView=[[UIAlertView alloc]initWithTitle:@"提示" message:str delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
            [alertView show];
            
        }else{
            [SVProgressHUD showErrorWithStatus:@"操作错误"];
        }
    } failure:^(NSHTTPURLResponse *response, NSError *error) {
        [SVProgressHUD showErrorWithStatus:@"网络错误"];
    }];
}

-(void)chickInputButton:(UIButton *)sender{
    //补登
    [[BuaatVolunteerServerSDK sharedInstance]requestQrcodeActivityInfoWithActivityCode:self.resultDic[@"activityCode"] success:^(NSHTTPURLResponse *response, id responseObject) {
        if ([responseObject isKindOfClass:[NSDictionary class]]) {
            [SVProgressHUD dismiss];
            id result = responseObject[@"result"];
            if ([result isKindOfClass:[NSDictionary class]]) {
                
                [self pushWithActivity:result];
                
                
                
            }else
            {
                [SVProgressHUD showErrorWithStatus:@"没有该活动信息" maskType:SVProgressHUDMaskTypeBlack];
            }
        }else
        {
            [SVProgressHUD showErrorWithStatus:@"没有该活动信息" maskType:SVProgressHUDMaskTypeBlack];
        }
        //        [self pushInput];
        
    } failure:^(NSHTTPURLResponse *response, NSError *error) {
        [SVProgressHUD showErrorWithStatus:@"网络错误" maskType:SVProgressHUDMaskTypeBlack];
    }];
    
}

-(void)pushWithActivity:(NSDictionary *)result
{
    VolunteerInputViewController *vInputVC = [[VolunteerInputViewController alloc]initWithNibName:@"VolunteerInputViewController" bundle:nil];
    [vInputVC setActivityDic:result];
    vInputVC.isAlreadyInputActiveCode = YES;
    [self.navigationController pushViewController:vInputVC animated:YES];
}

-(void)chickSignInButton:(UIButton *)sender{
    //活动签到
    ModelUser *user=[UserInfoManager sharedManager].modelMser;
    NSDateFormatter *f=[[NSDateFormatter alloc]init];
    [f setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];
    [f setDateFormat:@"yyyyMMdd HHmmss"];
    NSString *dateStr=[f stringFromDate:[NSDate date]];
    [SVProgressHUD show];
    [[BuaatVolunteerServerSDK sharedInstance]requestActivitySignWithActivityCode:self.activityCode volunteerCode:user.volunteerCode signType:@"1" signTime:dateStr signLongitude:@"" signDimensions:@"" success:^(NSHTTPURLResponse *response, id responseObject) {
        if([responseObject[@"ret"] intValue]==0){
            [SVProgressHUD dismiss];
            NSString *str=[[NSString alloc]initWithFormat:@"签到成功\n签到号:%@",responseObject[@"result"][@"signId"]];
            UIAlertView *alertView=[[UIAlertView alloc]initWithTitle:@"提示" message:str delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
            [alertView show];
        
        }else{
            [SVProgressHUD showErrorWithStatus:@"操作错误"];
        }
    } failure:^(NSHTTPURLResponse *response, NSError *error) {
        [SVProgressHUD showErrorWithStatus:@"网络错误"];
    }];
    
}

#pragma mark - tableView delegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if(section==0)
        return self.data.count;
    else if (section==2)
        return self.buttonTitles.count;
    else
        return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *CellIdentifier=@"CellIdentifier";
    static NSString *CellIdentifierButton=@"CellIdentifierButton";
    static NSString *CellIdentifierWhite=@"CellIdentifierWhite";
    if(indexPath.section==0){
        InfoTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
        NSDictionary *d=self.data[indexPath.row];
        cell.keyLabel.text=d[@"key"];
        cell.nameLabel.text=d[@"name"];
        return cell;
    }else if(indexPath.section==2){
        ActivityInfoTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifierButton forIndexPath:indexPath];
        cell.delegate=self;
        [cell.button setTitle:self.buttonTitles[indexPath.row] forState:UIControlStateNormal];
        return cell;
    }else{
        ActivityInfoWhiteTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifierWhite forIndexPath:indexPath];
        return cell;
    }
    
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if(indexPath.section==0){
        if(indexPath.row==self.data.count-1){
            float height=[Utils heightForLabel:self.data[indexPath.row][@"groupName"] andFont:17.0f andWidth:SCREEN_WIDTH-100-12-8];
            if(height>50){
                return height;
            }else{
                return 50;
            }
        }
        return 50;
    }else if(indexPath.section==2){
        return 9+30*SCREEN_WIDTH/320;
    }else{
        if(SCREEN_HEIGHT==480){
            return 0;
        }else if (SCREEN_HEIGHT==568){
            return 100*SCREEN_WIDTH/375;

        }else if (SCREEN_HEIGHT>568){
            return 140*SCREEN_WIDTH/375;
        }else{
            return 100*SCREEN_WIDTH/375;

        }
    }
}

#pragma mark - ActivityInfoTableViewCell
-(void)chickButtonWithCell:(ActivityInfoTableViewCell *)cell{
    NSIndexPath *indexPath=[self.tableView indexPathForCell:cell];
    if(indexPath.section==2){
        switch (indexPath.row) {
            case 0:{
                [self chickInputButton:nil];
            }
                break;
            case 1:{
                [self chickApplyButton:nil];
            }
                break;
            case 2:{
                [self chickSignInButton:nil];
            }
                break;
            case 3:{
                [self chickExitButton:nil];
            }
                break;
        }
    }
}
@end
